package com.uit.trip.tab_activity.childs;

import java.io.File;

import com.uit.trip.R;
import com.uit.trip.TripApplication;
import com.uit.trip.bridge.Server;
import com.uit.trip.models.LocationModel;
import com.uit.trip.utils.ProcessImage;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Form_PostActivity extends Activity {

	private String mSelectedImagePath;
	private ImageView imgContent;
	private Bitmap bitMap;
	private Server server;
	private TextView tvAddress;
	private EditText etDescription;
	private TripApplication app;
	private LocationModel locationModel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.form_post);
		imgContent = (ImageView) findViewById(R.id.img_post);
		tvAddress = (TextView) this.findViewById(R.id.tvLocationDefault);
		etDescription = (EditText) this.findViewById(R.id.etDescription);
		app = (TripApplication) getApplication();
		locationModel = app.getLocation();
		if (locationModel != null) {
			Log.i("duongnx", "form_post::" + locationModel.address);
			if (locationModel.address != null) {
				tvAddress.setText("" + locationModel.address);
			}
		}
		server = new Server(this);

		Bundle extras = getIntent().getExtras();
		if (extras == null)
			return;
		mSelectedImagePath = extras.getString("imagepath");
		// Log.i("duongnx", "mSelectedImagePath::" + mSelectedImagePath);
		bitMap = null;
		if (mSelectedImagePath != null) {

			bitMap = ProcessImage.getInstance(this).getBitmapFromFile(
					mSelectedImagePath);
			bitMap = ProcessImage.getInstance(this).resizeBitmap(bitMap, 530,
					530);
			Log.i("duongnx", "bitmap:" + bitMap.getHeight());
			ProcessImage.getInstance(this).writeBitmapToFile(bitMap,
					mSelectedImagePath, "png");
			imgContent.setImageBitmap(bitMap);
		}
	}

	public void showPostAlertDialog() {
		AlertDialog.Builder builder = new Builder(Form_PostActivity.this);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle("Upload photo");
		builder.setCancelable(true);

		builder.setMessage("Your photo will be auto uploading...");
		builder.setNegativeButton("Ok", new OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				String userId = app.getUser().userId;
				String description = etDescription.getText().toString();
				String la = Double.toString(locationModel.location
						.getLatitude());
				String lng = Double.toString(locationModel.location
						.getLongitude());
				String address = locationModel.address;
				// DateFormat d = DateFormat.getDateTimeInstance();
				// String timeStamp = d.format(new Date());
				if (userId != null) {
					server.postPhoto(userId, description, la, lng, address,
							mSelectedImagePath);
				}
				finish();
			}
		});
		builder.setPositiveButton("Cancel", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.post_img:
			showPostAlertDialog();
			break;

		default:
			break;
		}
	}
}
